package com.agricultural.facade;

import com.agricultural.command.*;
import com.agricultural.emergency.EmergencyHandlerChain;
import com.agricultural.environment.EnvironmentMonitor;
import com.agricultural.equipment.AgriculturalEquipment;
import com.agricultural.equipment.EquipmentFactory;
import com.agricultural.observer.AlertSystem;
import com.agricultural.observer.EnvironmentDataLogger;
import com.agricultural.observer.EquipmentMonitor;
import com.agricultural.pathplanning.PathPlanningContext;
import com.agricultural.pathplanning.StandardPathPlanner;
import com.agricultural.security.SecurityManager;

import java.util.HashMap;
import java.util.Map;

public class AgriculturalSystemFacade {
    private Map<String, AgriculturalEquipment> equipmentMap = new HashMap<>();
    private CommandInvoker commandInvoker = new CommandInvoker();
    private EmergencyHandlerChain emergencyHandlerChain = new EmergencyHandlerChain();
    private EnvironmentMonitor environmentMonitor = new EnvironmentMonitor();
    private PathPlanningContext pathPlanningContext = new PathPlanningContext(new StandardPathPlanner());
    private SecurityManager securityManager = SecurityManager.getInstance();

    public void initializeSystem() {
        if (!securityManager.hasPermission("ADMIN")) return;
        
        System.out.println("初始化智能农机系统...");
        
        // 初始化环境监测器并添加观察者
        environmentMonitor.addObserver(new EnvironmentDataLogger());
        environmentMonitor.addObserver(new AlertSystem());

        
        System.out.println("系统初始化完成");
    }

    public void shutdownSystem() {
        if (!securityManager.hasPermission("ADMIN")) return;
        
        System.out.println("关闭智能农机系统...");
        environmentMonitor.stopMonitoring();
        System.out.println("系统已关闭");
    }

    public void createEquipment(String type, String id) {
        if (!securityManager.hasPermission("OPERATOR")) return;
        
        System.out.println("创建" + type + "设备: " + id);
        AgriculturalEquipment equipment = EquipmentFactory.createEquipment(type, id);
        equipmentMap.put(id, equipment);
        
        // 为设备添加监控器
        EquipmentMonitor monitor = new EquipmentMonitor("控制台监控器");
        equipment.addObserver(monitor);
    }

    public void startEquipment(String equipmentId) {
        if (!securityManager.hasPermission("OPERATOR")) return;
        
        AgriculturalEquipment equipment = equipmentMap.get(equipmentId);
        if (equipment != null) {
            Command startCommand = new StartCommand(equipment);
            commandInvoker.executeCommand(startCommand);
        } else {
            System.out.println("设备不存在: " + equipmentId);
        }
    }

    public void stopEquipment(String equipmentId) {
        if (!securityManager.hasPermission("OPERATOR")) return;
        
        AgriculturalEquipment equipment = equipmentMap.get(equipmentId);
        if (equipment != null) {
            Command stopCommand = new StopCommand(equipment);
            commandInvoker.executeCommand(stopCommand);
        } else {
            System.out.println("设备不存在: " + equipmentId);
        }
    }

    public void performTask(String equipmentId) {
        if (!securityManager.hasPermission("OPERATOR")) return;
        
        AgriculturalEquipment equipment = equipmentMap.get(equipmentId);
        if (equipment != null) {
            Command taskCommand = new PerformTaskCommand(equipment);
            commandInvoker.executeCommand(taskCommand);
        } else {
            System.out.println("设备不存在: " + equipmentId);
        }
    }

    public void refuelEquipment(String equipmentId) {
        if (!securityManager.hasPermission("OPERATOR")) return;
        
        AgriculturalEquipment equipment = equipmentMap.get(equipmentId);
        if (equipment != null) {
            Command refuelCommand = new RefuelCommand(equipment);
            commandInvoker.executeCommand(refuelCommand);
        } else {
            System.out.println("设备不存在: " + equipmentId);
        }
    }

    public void handleEmergency(String equipmentId, String emergencyType) {
        if (!securityManager.hasPermission("OPERATOR")) return;
        
        AgriculturalEquipment equipment = equipmentMap.get(equipmentId);
        if (equipment != null) {
            System.out.println("处理紧急情况: " + emergencyType + " 设备: " + equipmentId);
            emergencyHandlerChain.handleEmergency(equipment, emergencyType);
        } else {
            System.out.println("设备不存在: " + equipmentId);
        }
    }

    public void switchPathPlanner(boolean isWetField) {
        if (!securityManager.hasPermission("OPERATOR")) return;
        
        if (isWetField) {
            System.out.println("切换到湿地路径规划器");
            pathPlanningContext.setPathPlanner(new com.agricultural.pathplanning.WetFieldPathPlanner());
        } else {
            System.out.println("切换到标准路径规划器");
            pathPlanningContext.setPathPlanner(new com.agricultural.pathplanning.StandardPathPlanner());
        }
    }

    public void calculatePath(double[][] fieldBoundary, double[] startPosition) {
        if (!securityManager.hasPermission("OPERATOR")) return;
        
        System.out.println("计算作业路径...");
        pathPlanningContext.calculatePath(fieldBoundary, startPosition);
    }


    public void showCommandHistory() {
        if (!securityManager.hasPermission("VIEWER")) return;
        
        commandInvoker.showHistory();
    }

    public void startMonitoring() {
        environmentMonitor.startMonitoring();
    }
    public void stopMonitoring() {
        environmentMonitor.stopMonitoring();
    }
}    